package work;

import controller.CitationController;
import entity.text.ChapterEntity;
import entity.text.PaperEntity;
import util.TextRankKeywordsSimilarity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：hzs
 * @date ：Created in 2021/2/14 16:55
 * @description：判断文章的哪一个知识来源比较重要
 * @modified By：
 * @version: $
 */
public class Work2 {

    private final static String path = "Knowledge flow network planning and simulation";
    private CitationController citationController = new CitationController();
    private static String pdfFilePath = "G:\\pdfpaper\\ai\\%s.pdf";
    private static String txtFilePath = "G:\\AITXT\\1-s2.0-%s-main.txt";
    private static String xmlFilePath = "D:\\paper_database\\ai\\%s.xml";

    private PaperEntity getPaperEntityByCode(String fileCode) {
        File file = new File(String.format(xmlFilePath, fileCode));
        PaperEntity paperEntity = citationController.readPaperFromFile(file);
        return paperEntity;
    }

    /**
     * 计算每一个引用索引出现的次数
     * @param paperEntity
     * @return
     */
    private List<Integer> countCitationIndex(PaperEntity paperEntity) {
        List<Integer> result = new ArrayList<>();
        int i = 1;
        int notfound = 0;
        while (true) {
            int count = countCitationIndex(paperEntity, i++);
            result.add(count);
            if (count == 0) {
                notfound++;
                if (notfound > 2) {
                    break;
                }
            } else {
                notfound = 0;
            }
        }
        return result;
    }

    private int countCitationIndex(PaperEntity paperEntity, int index) {
        int count = 0;
        List<ChapterEntity> chapterEntities = paperEntity.getChapterEntities();
        Pattern compile = Pattern.compile("\\[(\\d+,)?" + index + "(,\\d+)?\\]");
        for (ChapterEntity chapterEntity : chapterEntities) {
            String content = chapterEntity.getContent();
            Matcher matcher = compile.matcher(content);
            while (matcher.find()) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Work2 work2 = new Work2();
        PaperEntity paperEntity = work2.getPaperEntityByCode(path);
        List<String> keywords = TextRankKeywordsSimilarity.getKeywordsList(paperEntity);
        System.out.println(keywords);
    }

}
